package com.hymjweb.demo.framework.sys;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.util.BeanUtil;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentPojo;
import com.hymjweb.demo.hymj.department_manage.service.XtDepartmentService;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BaseService {

    @Autowired
    private XtDepartmentService xtDepartmentService;


    public Long getSysId() {
        return Long.parseLong(DateUtil.getSysIdLocal());
    }


    public String getSysStrId() {
        return DateUtil.getSysIdLocal();
    }

    public Date getSysTime() {
        return DateUtil.getSysDateTime();
    }


    public HttpSession getSession() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        return request.getSession();
    }

    // 从Session中取出当前登录的对象
    public XtUserPojo getSystemUserPojo() {
        // 获取当前请求的Request
        HttpSession session = getSession();
        XtUserPojo pojo = (XtUserPojo) session.getAttribute(SysConstant.SESSION_LOGIN_USER_KEY);
        return pojo;
    }

    // 从Session中取出当前登录对象的归属部门
    public XtDepartmentPojo getSystemUserDepartment() {
        HttpSession session = getSession();
        XtDepartmentPojo pojo = (XtDepartmentPojo) session.getAttribute(SysConstant.SESSION_LOGIN_USER_DEPARTMENT_KEY);
        return pojo;
    }


    /**
     * 登录用户信息更新，直接修改 session 中的对象
     *
     * @param xtUserPojo
     */
    public void updateSessionLoginUserPojo(XtUserPojo xtUserPojo) {
        HttpSession session = getSession();
        session.setAttribute(SysConstant.SESSION_LOGIN_USER_KEY, xtUserPojo);

        XtDepartmentPojo departmentPojo = xtDepartmentService.getDepartmentPojoByUserId(xtUserPojo.getId());
        session.setAttribute(SysConstant.SESSION_LOGIN_USER_DEPARTMENT_KEY, departmentPojo);
    }

    public String getLoginCzyId() {
        XtUserPojo pojo = getSystemUserPojo();
        return pojo.getId();
    }

    public String getLoginCzyName() {
        XtUserPojo pojo = getSystemUserPojo();
        return pojo.getName();
    }

    public String getLoginCzyMobile() {
        XtUserPojo pojo = getSystemUserPojo();
        return pojo.getMobile();
    }

    public void pageListInit(Map map) {
        int pageNum = DataTypeUtil.toInt(map.get("pageIndex"), 0) + 1;
        int pageSize = DataTypeUtil.toInt(map.get("pageSize"), Integer.MAX_VALUE);
        String sortField = DataTypeUtil.toNotNullString(map.get("sortField"));
        String sortOrder = DataTypeUtil.toNotNullString(map.get("sortOrder"));
        String[] sortArray = null;
        String[] orderArray = null;
        if (sortField != null && !"".equals(sortField)) {
            sortArray = sortField.split(",");
        }
        if (sortOrder != null && !"".equals(sortOrder)) {
            orderArray = sortOrder.split(",");
        }
        StringBuffer orderBuffer = new StringBuffer();
        if (sortArray != null && orderArray != null && sortArray.length == orderArray.length) {
            // 开始转换
            for (int i = 0; i < sortArray.length; i++) {
                orderBuffer.append(BeanUtil.convertPropertyToDbColumnName(BeanUtil.convertPropertyToDbColumnName(sortArray[i])));
                orderBuffer.append(" ");
                orderBuffer.append(orderArray[i]);
                if (i < sortArray.length - 1) {
                    orderBuffer.append(",");
                }
            }
        }

        if (orderBuffer.length() > 0) {
            // 分页 排序
            PageHelper.startPage(pageNum, pageSize, orderBuffer.toString());
        } else {
            // 分页
            PageHelper.startPage(pageNum, pageSize);
        }
    }

    /**
     * 将list转成带分页的MIniUIGridResultMap
     *
     * @param list
     * @return
     */
    public MIniUIGridResultMap pageToGrid(List list) {
        PageInfo pageInfo = new PageInfo(list);
        MIniUIGridResultMap resultMap = new MIniUIGridResultMap(pageInfo);
        return resultMap;
    }

    /**
     * 对象转为JSONObject
     *
     * @param object
     * @return
     */
    public Object objectToJSONObject(Object object) {
        return JSONObject.toJSON(object);
    }

    /**
     * 设置查询参数操作员与部门 ID
     *
     * @param param
     */
    public void setQueryCommParam(Map param) {
        param.put("czyId", getLoginCzyId());
        param.put("departmentId", getSystemUserDepartment().getId());
    }


    public void throwException(String exceptionDesc) throws Exception {
        throw new Exception(exceptionDesc);
    }
}
